package com.junbao.message.infrastructure.dto;

import java.io.Serializable;
import java.sql.Timestamp;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;

import com.junbao.message.domain.enums.ConfirmWay;
import com.junbao.message.domain.enums.IsDead;
import com.junbao.message.domain.enums.NotifyDataType;
import com.junbao.message.domain.enums.NotifyStatus;
import com.junbao.message.domain.enums.NotifyWay;
import com.junbao.message.domain.model.NotifyMessage;

/**
 * 通知消息
 * 
 * @author yilin
 *
 */
public class NotifyMessageDto implements Serializable{

	private static final long serialVersionUID = -2937053824816980961L;
	
	private String uuid;
	//消息编号
	private String messageNo;
	// 消息序列号
	private String messageSquece;
	// 通知方式
	private int notifyWay;
	// 通知地址(REST)
	private String notifyUrl;
	// 通知数据类型
	private int notifyDataType;
	// 通知数据
	private String notifyData;
	// 消息确认次数
	private Integer confirmCount;
	// 最大重试次数
	private Integer retryMaxCount;
	// 重试次数
	private Integer retryCount;
	// 下一次重试时间
	private Date nextRetryTime;
	// 通知状态
	private int notifyStatus;
	// 是否死亡
	private int isDead;
	// 失败备注
	private String failRemark;
	// 待确认消息确认方式
	private int confirmWay;
	// 待确认消息确认请求地址
	private String confirmReqUrl;
	private Date createDate;
	private Date updateDate;
	private Integer deleteFlag;
	
	public NotifyMessageDto() {}
	
	public NotifyMessageDto(NotifyMessage notifyMessage) {
		this.uuid = notifyMessage.getUuid();
		this.messageNo = notifyMessage.getMessageNo();
		this.messageSquece = notifyMessage.getMessageSquece();
		this.notifyWay = notifyMessage.getNotifyWay().getCode();
		this.notifyUrl = notifyMessage.getNotifyUrl();
		this.notifyDataType = notifyMessage.getNotifyDataType().getCode();
		this.notifyData = notifyMessage.getNotifyData();
		this.confirmCount = notifyMessage.getConfirmCount();
		this.retryMaxCount = notifyMessage.getRetryMaxCount();
		this.retryCount = notifyMessage.getRetryCount();
		this.nextRetryTime = notifyMessage.getNextRetryTime();
		this.notifyStatus = notifyMessage.getNotifyStatus().getCode();
		this.isDead = notifyMessage.getIsDead().getCode();
		this.failRemark = notifyMessage.getFailRemark();
		this.confirmWay = notifyMessage.getConfirmWay() == null ? 0 : notifyMessage.getConfirmWay().getCode();
		this.confirmReqUrl = notifyMessage.getConfirmReqUrl();
		this.createDate = notifyMessage.getCreateDate();
		this.updateDate = notifyMessage.getUpdateDate();
		this.deleteFlag = notifyMessage.getDeleteFlag();
	}

	public String getUuid() {
		return uuid;
	}

	public void setUuid(String uuid) {
		this.uuid = uuid;
	}

	public String getMessageNo() {
		return messageNo;
	}

	public void setMessageNo(String messageNo) {
		this.messageNo = messageNo;
	}

	public String getMessageSquece() {
		return messageSquece;
	}

	public void setMessageSquece(String messageSquece) {
		if (StringUtils.isEmpty(messageSquece)) {
			throw new NullPointerException("消息序列为空!");
		}
		this.messageSquece = messageSquece;
	}

	public int getNotifyWay() {
		return notifyWay;
	}

	public void setNotifyWay(Integer notifyWay) {
		if (notifyWay == null) {
			throw new NullPointerException("消息通知方式为空!");
		}
		this.notifyWay = notifyWay;
	}

	public String getNotifyUrl() {
		return notifyUrl;
	}

	public void setNotifyUrl(String notifyUrl) {
		this.notifyUrl = notifyUrl;
	}

	public String getNotifyData() {
		return notifyData;
	}

	public void setNotifyData(String notifyData) {
		this.notifyData = notifyData;
	}

	public Date getNextRetryTime() {
		return nextRetryTime;
	}

	public Timestamp getNextRetryTimeFormatt(){
		return this.nextRetryTime == null ? null : new Timestamp(this.nextRetryTime.getTime());
	}
	
	public void setNextRetryTime(Date nextRetryTime) {
		this.nextRetryTime = nextRetryTime;
	}

	public int getNotifyDataType() {
		return notifyDataType;
	}

	public void setNotifyDataType(Integer notifyDataType) {
		this.notifyDataType = notifyDataType;
	}


	public Integer getRetryMaxCount() {
		return retryMaxCount;
	}

	public void setRetryMaxCount(Integer retryMaxCount) {
		this.retryMaxCount = retryMaxCount;
	}

	public Integer getRetryCount() {
		return retryCount;
	}

	public void setRetryCount(Integer retryCount) {
		this.retryCount = retryCount;
	}

	public int getNotifyStatus() {
		return notifyStatus;
	}

	public void setNotifyStatus(Integer notifyStatus) {
		if (notifyStatus == null) {
			throw new NullPointerException("通知状态为空!");
		}
		this.notifyStatus = notifyStatus;
	}

	public int getIsDead() {
		return isDead;
	}

	public void setIsDead(Integer isDead) {
		if (isDead == null) {
			throw new NullPointerException("是否为死亡标识为空!");
		}
		this.isDead = isDead;
	}

	public Integer getConfirmCount() {
		return confirmCount;
	}

	public void setConfirmCount(Integer confirmCount) {
		this.confirmCount = confirmCount;
	}

	public void setNotifyWay(int notifyWay) {
		this.notifyWay = notifyWay;
	}

	public void setNotifyDataType(int notifyDataType) {
		this.notifyDataType = notifyDataType;
	}

	public void setNotifyStatus(int notifyStatus) {
		this.notifyStatus = notifyStatus;
	}

	public void setIsDead(int isDead) {
		this.isDead = isDead;
	}

	public void setConfirmWay(int confirmWay) {
		this.confirmWay = confirmWay;
	}

	public String getFailRemark() {
		return failRemark;
	}

	public void setFailRemark(String failRemark) {
		this.failRemark = failRemark;
	}

	public int getConfirmWay() {
		return confirmWay;
	}

	public void setConfirmWay(Integer confirmWay) {
		this.confirmWay = confirmWay;
	}
	public String getConfirmReqUrl() {
		return confirmReqUrl;
	}

	public void setConfirmReqUrl(String confirmReqUrl) {
		this.confirmReqUrl = confirmReqUrl;
	}

	public Date getCreateDate() {
		return createDate;
	}
	
	public Timestamp getCreateDateFormatt(){
		return this.createDate == null ? null : new Timestamp(this.createDate.getTime());
	}

	public void setCreateDate(Date createDate) {
		if (createDate == null) {
			throw new NullPointerException("创建时间为空!");
		}
		this.createDate = createDate;
	}

	public Date getUpdateDate() {
		return updateDate;
	}
	
	public Timestamp getUpdateDateFormatt(){
		return this.updateDate == null ? null : new Timestamp(this.updateDate.getTime());
	}

	public Timestamp getNowFormatt(){
		return  new Timestamp(new Date().getTime());
	}
	
	public void setUpdateDate(Date updateDate) {
		if (updateDate == null) {
			throw new NullPointerException("更新时间为空!");
		}
		this.updateDate = updateDate;
	}

	public Integer getDeleteFlag() {
		return deleteFlag;
	}

	public void setDeleteFlag(Integer deleteFlag) {
		this.deleteFlag = deleteFlag;
	}

	
	public NotifyMessage buildNotifyMessage(){
		NotifyMessage notifyMessage = new NotifyMessage(this.messageNo, NotifyWay.getNotifyWayByCode(this.notifyWay), this.notifyData, this.notifyUrl);
		notifyMessage.setUuid(this.uuid);
		notifyMessage.setMessageSquece(this.messageSquece);
		notifyMessage.buildRestNotify(this.notifyUrl).buildConfirmWay(ConfirmWay.getConfirmWayByCode(this.confirmWay)).buildConfirmWayToRest(this.confirmReqUrl);
		notifyMessage.setIsDead(IsDead.getIsDeadByCode(this.isDead));
		notifyMessage.setNotifyStatus(NotifyStatus.getNotifyStatusByCode(this.notifyStatus));
		notifyMessage.setNotifyDataType(NotifyDataType.getNotifyDataTypeByCode(this.notifyDataType));
		notifyMessage.setCreateDate(this.createDate);
		notifyMessage.setUpdateDate(this.updateDate);
		notifyMessage.setDeleteFlag(this.deleteFlag);
		notifyMessage.setConfirmCount(this.confirmCount);
		notifyMessage.setNextRetryTime(this.nextRetryTime);
		notifyMessage.setRetryCount(this.retryCount);
		notifyMessage.setRetryMaxCount(this.retryMaxCount);
		return notifyMessage;
	}
	
	public static List<NotifyMessage> buildNotifyMessages(final List<NotifyMessageDto> notifyMessageDtos){
		List<NotifyMessage> notifyMessages = new LinkedList<NotifyMessage>();
		for (NotifyMessageDto notifyMessageDto : notifyMessageDtos) {
			notifyMessages.add(notifyMessageDto.buildNotifyMessage());
		}
		return notifyMessages;
	}
}
